package com.future.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.domain.Notice;
import com.future.dto.MsgDto;
import com.future.dto.NoticeDto;
import com.future.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

/**
 * @description:
 * @author: 何晔
 * @time: 2022/6/23 10:34
 */
@RestController
@RequestMapping("notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;





    @RequestMapping("findnotice")
    public Page<Notice> findnotice(NoticeDto noticeDto){

        Date date=new Date();
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<>();

      // 控件 时间 不为空 , 在  区间
       if(noticeDto.getNoticepublishtime()!=null){
           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
           String    date1    =  sdf.format(noticeDto.getNoticepublishtime());
           String     date2   =  sdf.format(date);
           queryWrapper.between("noticepublishtime",date1,date2);

        }else {
           //控件时间为空时，就是 小于当前时间。
           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

           String     date2   =  sdf.format(date);


           queryWrapper.le("noticepublishtime",date2);
       }

       if(noticeDto.getNoticestatus()!=null&noticeDto.getNoticestatus()!=""){
           queryWrapper.eq("noticestatus",noticeDto.getNoticestatus());
       }

        Page<Notice> page=new Page<>(noticeDto.getCurrent(),noticeDto.getSize());
        this.noticeService.page(page,queryWrapper);
        return page;
    }


    @RequestMapping("addnotice")
    public MsgDto findnotice(@RequestBody Notice notice){
        notice.setNoticepublishtime(new Date());
         this.noticeService.save(notice);
     return new MsgDto(true,"添加成功");

    }


    @RequestMapping("updatenotice")
    public MsgDto updatenotice(@RequestBody Notice notice){
        notice.setNoticeupdatetime(new Date());
      this.noticeService.updateById(notice);
        System.out.println(notice);
        return new MsgDto(true,"修改成功");

    }




}
